package com.wondersgroup.framework.sample.component.form.action;

import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class UploadAction extends BaseAjaxAction
{
  private static final long serialVersionUID = -4848248679889814408L;
  private File[] upload;
  private String[] uploadFileName;
  private String[] uploadContentType;

  public String doUpload()
    throws Exception
  {
    String realPath = ServletActionContext.getServletContext().getRealPath("");

    for (int i = this.upload.length - 1; i >= 0; --i) {
      File destFile = new File(realPath + "\\destFile\\" + this.uploadFileName[i]);
      try
      {
        FileUtils.copyFile(this.upload[i], destFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    createJSonData("{\"success\":true,\"msg\":\"file upload success\"}");

    return "ajax";
  }

  public void setUpload(File[] upload) {
    this.upload = upload;
  }

  public File[] getUpload() {
    return this.upload;
  }

  public void setUploadFileName(String[] uploadFileName) {
    this.uploadFileName = uploadFileName;
  }

  public String[] getUploadFileName() {
    return this.uploadFileName;
  }

  public void setUploadContentType(String[] uploadContentType) {
    this.uploadContentType = uploadContentType;
  }

  public String[] getUploadContentType() {
    return this.uploadContentType;
  }
}